package com.example.utils;

import java.util.HashMap;
import java.util.Map;

public class GetDeviceAttribute {

    public static String getDeviceAttribute(String projectId, String deviceId, String url, String serviceID, String nodeId, String token) throws Exception {
        // 填充查询路径
        url = String.format(url, projectId, deviceId);
        // 指定数据格式并附上token令牌
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("X-Auth-Token", token);
        // 填充查询参数
        Map<String, String> params = new HashMap<>();
        params.put("service_id", serviceID);
        params.put("node_id", nodeId);
        try{
            // 发送请求
            HttpUtils httpUtils = new HttpUtils();
            String responseBody = httpUtils.doGetWithHeaders(url, headers, params);
            if (responseBody == null || responseBody.isEmpty()) {
                return null;
            } else {
                //判断返回的状态码是否是令牌过期
                IsValidToken.isValidToken(responseBody);
                return responseBody;
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
}
